import SwiftUI
import SwiftData
import Alamofire

// Login/login
struct RequestData_Login: Codable {
    let phoneNumber: String
    let password: String
}

struct ResponseData_Login: Codable {
    let status: Bool
    let description: String
    let userId: String
}

class LoginloginRequester: ObservableObject {
    @Published var requestStatus: RequestStatus = .ready
    @Published var publishedResult: Bool = false
    
    func request(_ modelContext: ModelContext, _ request_phoneNumberInput: String, _ request_passwordInput: String) {
        // 准备数据
        let requestData = RequestData_Login(phoneNumber: request_phoneNumberInput,
                                            password: request_passwordInput)
        
        // 准备URL
        let requestURL = "http://62.234.45.120:1230/Login/login"
        
        // 发起请求
        self.requestStatus = .requesting
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_Login.self) { response in
            if let responseData = response.value {
                if responseData.status {
                    let loginInfo = LoginInfo(phoneNumber: request_phoneNumberInput,
                                              userId: responseData.userId)
                    modelContext.insert(loginInfo)
                } else {
                    showLoginAlert(description: responseData.description)
                }
                self.publishedResult = responseData.status
                self.requestStatus = .finish
            }
        }
    }
}

// Login/register
struct RequestData_Register: Codable {
    let phoneNumber: String
    let password: String
}

struct ResponseData_Register: Codable {
    let status: Bool
    let description: String
}

class LoginregisterRequester: ObservableObject {
    @Published var requestStatus: RequestStatus = .ready
    @Published var publishedResult: (status: Bool, description: String) = (false, "未知错误")
    
    func request(_ request_phoneNumberInput: String, _ request_passwordInput: String) {
        // 准备数据
        let requestData = RequestData_Register(phoneNumber: request_phoneNumberInput,
                                               password: request_passwordInput)
        
        // 准备URL
        let requestURL = "http://62.234.45.120:1230/Login/register"
        
        // 发起请求
        self.requestStatus = .requesting
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_Register.self) { response in
            if let responseData = response.value {
                self.publishedResult = (responseData.status, responseData.description)
                self.requestStatus = .finish
            }
        }
    }
}

// 使用URLSession实现版本
//    func request() {
//        let requestData = requestData_Login(phoneNumber: phoneNumberInput,
//                                            password: passwordInput)
//        guard let requestDatatJson = try? JSONEncoder().encode(requestData) else {
//            return
//        }
//
//        let requestURL = URL(string: "http://62.234.45.120:1230/Login/login")!
//
//        var request = URLRequest(url: requestURL)
//        request.httpMethod = "POST"
//        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
//
//        let loginTask = URLSession.shared.uploadTask(with: request, from: requestDatatJson)
//        { data, response, error in
//            if let dataUnwrapped = data {
//                let responseData = try! JSONDecoder().decode(ResponseData_Login.self, from: dataUnwrapped)
//
//                if responseData.status {
//                    let loginInfo = LoginInfo(phoneNumber: phoneNumberInput,
//                                              userId: responseData.userId)
//                    modelContext.insert(loginInfo)
//                }
//            }
//        }
//
//        loginTask.resume()
//    }
